package com.bawei.demo_shoppingcar.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bawei.demo_shoppingcar.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    List<Integer> listhome = new ArrayList<>();
    private ImageView naving_image;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            startActivity();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences demo_shopping = getSharedPreferences("demo_shopping", MODE_PRIVATE);
        int id = demo_shopping.getInt("ID", 0);
        initView();
        if (id == 0) {
            //第一次进入APP
            naving_image.setVisibility(View.GONE);
            viewPager.setVisibility(View.VISIBLE);
            initViewPager();
            monitorViewPager();
            demo_shopping.edit().putInt("ID", 1).commit();
        } else if (id == 1) {
            //不是第一次进入APP
            viewPager.setVisibility(View.GONE);
            naving_image.setVisibility(View.VISIBLE);
            mHandler.sendEmptyMessageDelayed(0, 2000);
        }
    }

    private void monitorViewPager() {
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        int currentItem = viewPager.getCurrentItem();

                        if (currentItem == listhome.size() - 1) {
                            startActivity();
                        }
                        break;
                }
                return false;
            }
        });
    }

    /**
     * 跳转页面
     */
    private void startActivity() {
        Intent intent = new Intent(MainActivity.this, ContentActivity.class);
        startActivity(intent);
        finish();
    }

    private void initViewPager() {
        listhome.add(R.drawable.a);
        listhome.add(R.drawable.b);
        listhome.add(R.drawable.c);
        listhome.add(R.drawable.d);
        listhome.add(R.drawable.e);

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                ImageView image = new ImageView(MainActivity.this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                image.setLayoutParams(params);
                image.setImageResource(listhome.get(position));
                container.addView(image);
                return image;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return listhome.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        naving_image = (ImageView) findViewById(R.id.naving_image);
    }
}
